<?php 
	// get请求的函数
	function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);
 
    return $res;
  }

  //第一步1、 取出code 
	
	mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
    mysql_select_db(SAE_MYSQL_DB);

   function getuser_access_token(){
      $code = $_GET["code"];
	$appID = "wxca6cc5eb15e706d5";
	$appsecret ="e3b46505fd8a28bfe101efb95d7fdc24";
  $accessTokenAPI = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appID}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; 
$rs = httpGet($accessTokenAPI);
	 //    // 把获取到json串转化为 php对象
	    $array1 = json_decode($rs,true);		         
	  $token = $array1["access_token"];
		 $openID = $array1["openid"];
	
	 return array("token"=>$token,"openID"=>$openID);;





	 //    $findrs = mysql_query("SELECT *FROM snsapi_accessToken");
	 //   $accessTokenAPI = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appID}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; 
	 //  if (mysql_num_rows($findrs) > 0) {
	 //    $row = mysql_fetch_assoc($findrs);
	 //    // 判断是否超时
	 //    $nowTime = time();
	 //    $dis = $nowTime - $row["time"];
	 //    if ($dis >= 7200) {
	 //      // 重新请求
	 //    $rs = httpGet($accessTokenAPI);
	 //    $array1 = json_decode($rs,true);
	 //    $token = $array1["access_token"];
	 //    $openID = $array1["openid"];
	 //    mysql_query("UPDATE snsapi_accessToken SET access_token='$token',time='$nowTime',open_id='$openID'");
	 //      return array("token"=>$token,"openID"=>$openID);

	 //    }else{
	 //        //return array("accesstoken"=>$row["token"],"JsApiTicket"=>$row["jsapiticket"]);
	 //        return array("token"=>$row["access_token"],"openID"=>$row["open_id"]);
	 //    }


	 //  }else{
	 //    // 请求获取 token
	 //     // 获取 token
	 //    $rs = httpGet($accessTokenAPI);
	 //    // 把获取到json串转化为 php对象
	 //     $array1 = json_decode($rs,true);
		
         
	 //      $token = $array1["access_token"];
		// $openID = $array1["openid"];
	 //      // 获取当前时间
	 //      $t = time();
	 //      $f =  mysql_query("INSERT INTO snsapi_accessToken(access_token,time,open_id) VALUES ('$token','$t','$openID')");
	 //      //return array("accesstoken"=>$token,"JsApiTicket"=>$ticket);
	 //  return array("token"=>$token,"openID"=>$openID);;
	 //  }
  }
    
	$rsaArray = getuser_access_token();

	$access_token = $rsaArray["token"];
//	echo $access_token;
	$openID = $rsaArray["openID"];
	
	// 第三步：拉取用户信息
	$userAPI = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openID}&lang=zh_CN";
	$rsB = httpGet($userAPI);
	
	$rsBArray = json_decode($rsB,true);

	$userName = $rsBArray["nickname"];
	$head = $rsBArray["headimgurl"];
	
	// 把用户数据保存下来
	
	$sql1 = "SELECT *FROM user_ranking WHERE user_name='$userName'";
	$findrsResult = mysql_query($sql1);
	if (mysql_num_rows($findrsResult) > 0){
		$row = mysql_fetch_array($findrsResult);
		$score = $row["user_score"];
		$score += 100;
        mysql_query("UPDATE user_ranking SET user_score='$score' WHERE user_name='$userName'");
	}else{
		$insertSQL = "INSERT INTO user_ranking (user_name,user_header,user_score) VALUES('$userName','$head',100)";
		mysql_query($insertSQL);
	}

	// 查询当前所有分享过的用户
	$sql2 = "SELECT *FROM user_ranking ORDER BY user_score DESC";
	$allUserInfo = mysql_query($sql2);
	$userArray = array();
	while ($row = mysql_fetch_assoc($allUserInfo)) {
		$userArray[] = $row;
	}
 ?>
<!DOCTYPE html>
 <html>
 <meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
 <head>
 	<title>成绩排名</title>
 	<style>
 	h2,h5{
 		text-align:center;
 	}
 	span{
 		display:inline-block;
 		width: 20%;
 		height: 30px;
 		line-height: 30px;
 	}
 	img{
 		width: 30px;
 		height: 30px;
 	}
 	#box div{
 		text-align:center;
         height:30px;
         border-bottom:1px solid rgb(250,250,250);
 	}
    #box div .name{
 		position: absolute;
		left: 5%;
		text-align: left;
 	}
 	#box div .score{
 		position: absolute;
		right: 5%;
		text-align: right;
 	}
 	</style>
 </head>

 <body>
 		<h2>分享越多您的排名越高哦</h2>
 		<h5>全国排名</h5>
 		<div id="box">
 			
 		</div>
 </body>
 <script type="text/javascript">

 	var box = document.getElementById("box");

	var str =  "<?php 
		$array11 = array();
 		foreach ($userArray as $key => $value) {
 			$array11[] = implode('..',$value);
 		}
 		echo implode('||',$array11);

 	 ?>"
 	 var arr = str.split("||");
 	 for(var i = 0; i < arr.length;i++){
 	 	arr[i] = arr[i].split("..");
 	 }

 	 for(var i = 0;i < arr.length;i ++){
 	 	var user = arr[i];
 	 	var div = document.createElement("div");
 	 	box.appendChild(div);
 	 	var spanName = document.createElement("span");
 	 	spanName.className = "name";
 	 	spanName.innerHTML = user[1];
 	 	div.appendChild(spanName);

 	 	var img = document.createElement("img");
 	 	img.src = user[2];
 	 	div.appendChild(img);


 	 	var scoreSpan = document.createElement("span");
 	 	scoreSpan.className = 'score';
 	 	scoreSpan.innerHTML = user[3];
 	 	div.appendChild(scoreSpan);
 	 	
 	 }



 </script>
 </html>


 